package com.csw.shuanfa.CodeImprove.CompleteFutureLinkedBlockingQueue.oldtest.ThreadAndBingFa;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ThreadFactory;

/**
 * @author 1
 */
@Slf4j
public class CallableThreadTest implements Callable<Integer> {

    static int maxLength = 100;

    public static void main(String[] args) throws ExecutionException {
        CallableThreadTest ctt = new CallableThreadTest();
        FutureTask<Integer> ft = new FutureTask<>(ctt);
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("demo-pool-%d").build();
        for (int i = 0; i < maxLength; i++) {
            log.info("" + Thread.currentThread().getName() + " 的循环变量i的值" + i);
            if (i == 20) {
                new Thread(ft, "有返回值的线程").start();
            }
        }
        try {
            log.info("" + "子线程的返回值：" + ft.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
            Thread.currentThread().interrupt();
        }

    }

    @Override
    public Integer call() throws Exception {
        int i = 0;
        for (; i < maxLength; i++) {
            log.info("" + Thread.currentThread().getName() + " " + i);
        }
        return i;
    }
}
